function handles = mapStacker(handles, mode)
% mapStacker
%
% Private version for mapAnalysis2p0
% See makeMapDataArray for code to planes in mapDataArray
%
% See also makeMapDataArray
%
% Editing:
% gs april 2005
% ------------------------------------------------------------------------------------------------

switch mode
case 'reset'
    disp('  resetting mapStacks ... ');
    handles.data.map.mapStack.mapStackEC = [];
    handles.data.map.mapStack.mapStackED = [];
    handles.data.map.mapStack.mapStackIC = [];
    handles.data.map.mapStack.mapStackGenMin = [];
    handles.data.map.mapStack.mapStackGenMax = [];
    handles.data.map.mapStack.mapStackGenMean = [];
    handles.data.map.mapStack.mapStackGenOnset = [];
    handles.data.map.mapStack.mapStackGenMaxOnset = [];
    handles.data.map.mapStack.mapStackGenMean4th = [];
    handles.data.map.mapStack.mapStackGenMin4th = [];
    handles.data.map.mapStack.mapStackGenMax4th = [];
    handles.data.map.mapStack.mapAvgMin = [];
    handles.data.map.mapStack.mapAvgMax = [];
    handles.data.map.mapStack.mapAvgMean = [];
    handles.data.map.mapStack.mapAvgOnset = [];
    handles.data.map.mapStack.mapAvgMinED = [];
    handles.data.map.mapStack.mapAvgMeanED = [];
    handles.data.map.mapStack.mapAvgOnsetED = [];
    handles.data.map.mapStack.mapAvgMean4th = [];
    handles.data.map.mapStack.mapAvgMin4th = [];
    handles.data.map.mapStack.mapAvgMax4th = [];
    handles.data.map.mapStack.mapAvgVec = [];
    handles.data.map.mapStack.mapAvgHistoBase = [];
    handles.data.map.mapStack.mapAvgHistoSyn = [];
    handles.data.map.mapStack.mapStackLatHistoEdges = [];
    handles.data.map.mapStack.mapStackLatHistoN = [];
    handles.data.map.mapStack.mapStackHistoThresh = [];
    handles.data.map.mapStack.numMapsInStack = 0;
case 'add'
    disp('  adding map to mapStacks ... ');    
    [rows, cols, planes] = size(handles.data.map.mapStack.mapStackEC);
    if rows == 0
        newMapPlane = 1;
    else
        newMapPlane = planes+1;
    end
    handles.data.map.mapStack.mapStackEC(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,2); 
    handles.data.map.mapStack.mapStackED(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,3); 
    handles.data.map.mapStack.mapStackIC(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,4); 
    handles.data.map.mapStack.mapStackGenMin(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,5); 
    handles.data.map.mapStack.mapStackGenMean(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,9); 
    handles.data.map.mapStack.mapStackGenOnset(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,10); 
    handles.data.map.mapStack.mapStackGenMaxOnset(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,12); 
    handles.data.map.mapStack.mapStackGenMax(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,11); 
    handles.data.map.mapStack.mapStackHistoBase(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,13); 
    handles.data.map.mapStack.mapStackHistoSyn(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,14); 
    handles.data.map.mapStack.numMapsInStack = handles.data.map.mapStack.numMapsInStack + 1;
    handles.data.map.mapStack.mapStackGenMean4th(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,15); 
    handles.data.map.mapStack.mapStackGenMin4th(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,16); 
    handles.data.map.mapStack.mapStackGenMax4th(:,:,newMapPlane) = handles.data.map.mapActive.mapDataArray(:,:,17); 
case 'mean'
%     clc
    disp('  averaging maps in mapStack ... ');
    % strategy: 
    % in the map stacks, put NaNs at all ED positions;
    % average across the planes, ignoring EDs; 
    % define consistently ED positions as 'true' EDs
    
    % edStack
    edStack = handles.data.map.mapStack.mapStackED;
    
    % min
    gen = handles.data.map.mapStack.mapStackGenMin;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    genMin = mapAverage(gen, 1);
    disp('map.genMin = [');
    disp(genMin);
    disp('];');
    
    % max
    gen = handles.data.map.mapStack.mapStackGenMax;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    genMax = mapAverage(gen, 1);
    disp('map.genMax = [');
    disp(genMax);
    disp('];');
    
    % mean
    gen = handles.data.map.mapStack.mapStackGenMean;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    genMean = mapAverage(gen, 1);
    disp('map.genMean = [');
    disp(genMean);
    disp('];');
    
    % onset (min)
    gen = handles.data.map.mapStack.mapStackGenOnset;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    genOnset = mapAverage(gen, 1);
    disp('map.genOnset = [');
    disp(genOnset);
    disp('];');
    
    % onset (max)
    gen = handles.data.map.mapStack.mapStackGenMaxOnset;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    genMaxOnset = mapAverage(gen, 1);
    disp('map.genMaxOnset = [');
    disp(genMaxOnset);
    disp('];');
    
    % ed min
    gen = handles.data.map.mapStack.mapStackGenMin;
    allEd = mapAverage(gen, 1);
    trueEd = isnan(genOnset);
    ed = allEd.*trueEd;
    ed(ed==0)=NaN;
    edMin = ed;
    disp('map.edMin = [');
    disp(edMin);
    disp('];');
    
    % ed mean
    gen = handles.data.map.mapStack.mapStackGenMean;
    allEd = mapAverage(gen, 1);
    trueEd = isnan(genOnset);
    ed = allEd.*trueEd;
    ed(ed==0)=NaN;
    edMean = ed;
    disp('map.edMean = [');
    disp(edMean);
    disp('];');
    
    % ed onset
    gen = handles.data.map.mapStack.mapStackGenOnset;
    allEd = mapAverage(gen, 1);
    trueEd = isnan(genOnset);
    ed = allEd.*trueEd;
    ed(ed==0)=NaN;
    edOnset = ed;
    disp('map.edOnset = [');
    disp(edOnset)
    disp('];');
    
    % histo baseline
    gen = handles.data.map.mapStack.mapStackHistoBase;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    histoBase = mapAverage(gen, 1);
    
    % histo synaptic
    gen = handles.data.map.mapStack.mapStackHistoSyn;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    histoSyn = mapAverage(gen, 1);
    
    % mean4th % gs 2006 12 04
    gen = handles.data.map.mapStack.mapStackGenMean4th;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    genMean4th = mapAverage(gen, 1);
    disp('map.genMean4th = [');
    disp(genMean);
    disp('];');
    
    % min4th % gs 2006 12 04
    gen = handles.data.map.mapStack.mapStackGenMin4th;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    genMin4th = mapAverage(gen, 1);
    disp('map.genMin4th = [');
    disp(genMin);
    disp('];');
    
    % max4th % gs 2006 12 04
    gen = handles.data.map.mapStack.mapStackGenMax4th;
    gen(edStack~=0) = NaN; % set the EDs in the 'gen' maps to NaNs
    genMax4th = mapAverage(gen, 1);
    disp('map.genMax4th = [');
    disp(genMax);
    disp('];');
    
    handles.data.map.mapStack.mapAvgMin = genMin;
    handles.data.map.mapStack.mapAvgMax = genMax;
    handles.data.map.mapStack.mapAvgMean = genMean;
    handles.data.map.mapStack.mapAvgOnset = genOnset;
    handles.data.map.mapStack.mapAvgMaxOnset = genMaxOnset;
    handles.data.map.mapStack.mapAvgMinED = edMin;
    handles.data.map.mapStack.mapAvgMeanED = edMean;
    handles.data.map.mapStack.mapAvgOnsetED = edOnset;
    handles.data.map.mapStack.mapAvgHistoBase = histoBase;
    handles.data.map.mapStack.mapAvgHistoSyn = histoSyn;
    handles.data.map.mapStack.mapAvgMean4th = genMean4th;
    handles.data.map.mapStack.mapAvgMin4th = genMin4th;
    handles.data.map.mapStack.mapAvgMax4th = genMax4th;

    
%     state.uncaging.map.mapAvgEC = mapAverage(state.uncaging.map.mapStackEC, 1);
%     state.uncaging.map.mapAvgED = mapAverage(state.uncaging.map.mapStackED, 1);
%     state.uncaging.map.mapAvgIC = mapAverage(state.uncaging.map.mapStackIC, 1);
%     state.uncaging.map.mapAvgGenMin = mapAverage(state.uncaging.map.mapStackGenMin, 1);
%     mapAvg_EC = state.uncaging.map.mapAvgEC
%     mapAvg_ED = state.uncaging.map.mapAvgED
%     mapAvg_IC = state.uncaging.map.mapAvgIC
%     mapAvg_GenMin = state.uncaging.map.mapAvgGenMin
%     EDremover;
%     mapAvg_GenMinEDless = state.uncaging.map.mapAvgGenMinEDless

end
